/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hisense.settings.sound;

import com.android.settings.R;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.IPowerManager;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.SeekBarDialogPreference;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.hisense.settings.util.Wrapper;
//import com.marvell.tv.TVAVSettingsConstants;

public class UserEQPreference extends SeekBarDialogPreference implements
        SeekBar.OnSeekBarChangeListener{
    
    private static final String TAG = "UserEQPreference";
    private SeekBar mSeekBar120Hz;
    private SeekBar mSeekBar500Hz;
    private SeekBar mSeekBar1_5kHz;
    private SeekBar mSeekBar5kHz;
    private SeekBar mSeekBar10kHz;

    private int mOldUser120Hz;
    private int mOldUser500Hz;
    private int mOldUser1_5kHz;
    private int mOldUser5kHz;
    private int mOldUser10kHz;

    private int mOld120Hz;
    private int mOld500Hz;
    private int mOld1_5kHz;
    private int mOld5kHz;
    private int mOld10kHz;
    
	private int mOldAudioMode;

    private boolean mRestoredOldState;
    
    // value range is from -10 - 10. Need to make sure that user
    // doesn't set the value to 0 and get stuck
    private static final int MINIMUM_EQ = -10;
    private static final int MAXIMUM_EQ = 10;
    private TextView equalizer_120hz_textview;
    private TextView equalizer_500hz_textview;
    private TextView equalizer_1_5khz_textview;
    private TextView equalizer_5khz_textview;
    private TextView equalizer_10khz_textview;
    //private static int AUDIO_MODE_USER = TVAVSettingsConstants.TvAudioMode.TV_AUDIO_ModeUser
    //        .value();
    private static int AUDIO_MODE_USER = 0;
    
    private ContentObserver mValueObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
           onValueChanged();
        }
    };

    public UserEQPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setDialogLayoutResource(R.layout.preference_dialog_user_eq);
        Log.d(TAG, "UserEQPreference-->");
    }

    @Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);
        mRestoredOldState = false;
        Log.d(TAG, "showDialog-->");
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        
	//120hz
        mSeekBar120Hz = (SeekBar) view.findViewById(R.id.equalizer_120hz_seekbar);
        mSeekBar120Hz.setMax(MAXIMUM_EQ - MINIMUM_EQ);
        mOld120Hz = getEqValue_120Hz();
        Log.d(TAG, "getEqValue_120Hz-->" + mOld120Hz);

        mSeekBar120Hz.setProgress(mOld120Hz - MINIMUM_EQ);
        mSeekBar120Hz.setOnSeekBarChangeListener(this);

        equalizer_120hz_textview = (TextView) view.findViewById(R.id.equalizer_120hz_textview);
        equalizer_120hz_textview.setText("" + mOld120Hz);
        
	//500hz
        mSeekBar500Hz = (SeekBar) view.findViewById(R.id.equalizer_500hz_seekbar);
        mSeekBar500Hz.setMax(MAXIMUM_EQ - MINIMUM_EQ);
        mOld500Hz = getEqValue_500Hz();
        Log.d(TAG, "getEqValue_500Hz-->" + mOld500Hz);

        mSeekBar500Hz.setProgress(mOld500Hz - MINIMUM_EQ);
        mSeekBar500Hz.setOnSeekBarChangeListener(this);

        equalizer_500hz_textview = (TextView) view.findViewById(R.id.equalizer_500hz_textview);
        equalizer_500hz_textview.setText("" + mOld500Hz);
        
	//1.5khz
        mSeekBar1_5kHz = (SeekBar) view.findViewById(R.id.equalizer_1p5khz_seekbar);
        mSeekBar1_5kHz.setMax(MAXIMUM_EQ - MINIMUM_EQ);
        mOld1_5kHz = getEqValue_1500Hz();
        Log.d(TAG, "getEqValue_1500Hz-->" + mOld1_5kHz);

        mSeekBar1_5kHz.setProgress(mOld1_5kHz - MINIMUM_EQ);
        mSeekBar1_5kHz.setOnSeekBarChangeListener(this);

        equalizer_1_5khz_textview = (TextView) view.findViewById(R.id.equalizer_1p5khz_textview);
        equalizer_1_5khz_textview.setText("" + mOld1_5kHz);
        
	//5khz
        mSeekBar5kHz = (SeekBar) view.findViewById(R.id.equalizer_5khz_seekbar);
        mSeekBar5kHz.setMax(MAXIMUM_EQ - MINIMUM_EQ);
        mOld5kHz = getEqValue_5kHz();
        Log.d(TAG, "getEqValue_5kHz-->" + mOld5kHz);

        mSeekBar5kHz.setProgress(mOld5kHz - MINIMUM_EQ);
        mSeekBar5kHz.setOnSeekBarChangeListener(this);

        equalizer_5khz_textview = (TextView) view.findViewById(R.id.equalizer_5khz_textview);
        equalizer_5khz_textview.setText("" + mOld5kHz);
        
	//10khz
        mSeekBar10kHz = (SeekBar) view.findViewById(R.id.equalizer_10khz_seekbar);
        mSeekBar10kHz.setMax(MAXIMUM_EQ - MINIMUM_EQ);
        mOld10kHz = getEqValue_10kHz();
        Log.d(TAG, "getEqValue_10kHz-->" + mOld10kHz);

        mSeekBar10kHz.setProgress(mOld10kHz - MINIMUM_EQ);
        mSeekBar10kHz.setOnSeekBarChangeListener(this);

        equalizer_10khz_textview = (TextView) view.findViewById(R.id.equalizer_10khz_textview);
        equalizer_10khz_textview.setText("" + mOld10kHz);

        mOldAudioMode = Wrapper.getWrapperInstance().getAudioMode();
        if (mOldAudioMode != AUDIO_MODE_USER) {
            Log.d(TAG, "get Current Audio Mode-->" + Wrapper.getWrapperInstance().getAudioMode());
            Wrapper.getWrapperInstance().setAudioMode(AUDIO_MODE_USER);
        }
        
        mOldUser120Hz = getEqValue_120Hz();
        mOldUser500Hz = getEqValue_500Hz();
        mOldUser1_5kHz = getEqValue_1500Hz();
        mOldUser5kHz = getEqValue_5kHz();
        mOldUser10kHz = getEqValue_10kHz();

        Log.d(TAG, "onBindDialogView-->");
    }

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {    	

        Log.d(TAG, "onProgressChanged getaumode-->" + Wrapper.getWrapperInstance().getAudioMode());
        Log.d(TAG, "getEqValue 0f OldUser-->" + mOldUser120Hz + " " + mOldUser500Hz + " " + mOldUser1_5kHz + " " + mOldUser5kHz + " " + mOldUser10kHz);
        Log.d(TAG, "getprogress-->" + progress);

        if (seekBar == mSeekBar120Hz) {
            mOld120Hz = progress + MINIMUM_EQ;
            setEqValue_120Hz(mOld120Hz);
            equalizer_120hz_textview.setText("" + mOld120Hz);
        } else if (seekBar == mSeekBar500Hz) {
            mOld500Hz = progress + MINIMUM_EQ;
            setEqValue_500Hz(mOld500Hz);
            equalizer_500hz_textview.setText("" + mOld500Hz);
        } else if (seekBar == mSeekBar1_5kHz) {
            mOld1_5kHz = progress + MINIMUM_EQ;
            setEqValue_1500Hz(mOld1_5kHz);
            equalizer_1_5khz_textview.setText("" + mOld1_5kHz);
        } else if (seekBar == mSeekBar5kHz) {
            mOld5kHz = progress + MINIMUM_EQ;
            setEqValue_5kHz(mOld5kHz);
            equalizer_5khz_textview.setText("" + mOld5kHz);
        } else if (seekBar == mSeekBar10kHz) {
            mOld10kHz = progress + MINIMUM_EQ;
            setEqValue_10kHz(mOld10kHz);
            equalizer_10khz_textview.setText("" + mOld10kHz);
        }
        Log.d(TAG, "onProgressChanged-->progress=" + progress);
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        // NA
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        // NA
    }

    private void setEqValue_120Hz(int value) {
        Wrapper.getWrapperInstance().setBand1(value);
    }

    private int getEqValue_120Hz() {
    	int value = 0;
    	value = Wrapper.getWrapperInstance().getBand1();
        return value;
    }

    private void setEqValue_500Hz(int value) {
        Wrapper.getWrapperInstance().setBand2(value);
    }       

    private int getEqValue_500Hz() {
    	int value = 0;
    	value = Wrapper.getWrapperInstance().getBand2();
        return value;
    }

    private void setEqValue_1500Hz(int value) {
        Wrapper.getWrapperInstance().setBand3(value);
    }

    private int getEqValue_1500Hz() {
    	int value = 0;
    	value = Wrapper.getWrapperInstance().getBand3();
        return value;
    }

    private void setEqValue_5kHz(int value) {
        Wrapper.getWrapperInstance().setBand4(value);
    }

    private int getEqValue_5kHz() {
    	int value = 0;
    	value = Wrapper.getWrapperInstance().getBand4();
        return value;
    }

    private void setEqValue_10kHz(int value) {
        Wrapper.getWrapperInstance().setBand5(value);
    }

    private int getEqValue_10kHz() {
    	int value = 0;
    	value = Wrapper.getWrapperInstance().getBand5();
        return value;
    }

    private void onValueChanged() {
        mSeekBar120Hz.setProgress(getEqValue_120Hz() - MINIMUM_EQ);
        mSeekBar500Hz.setProgress(getEqValue_500Hz() - MINIMUM_EQ);
        mSeekBar1_5kHz.setProgress(getEqValue_1500Hz() - MINIMUM_EQ);
        mSeekBar5kHz.setProgress(getEqValue_5kHz() - MINIMUM_EQ);
        mSeekBar10kHz.setProgress(getEqValue_10kHz() - MINIMUM_EQ);
        Log.d(TAG, "onvalueChanged-->value=");
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        
        if (positiveResult) {
        	
            setEqValue_120Hz(mOld120Hz);
            setEqValue_500Hz(mOld500Hz);
            setEqValue_1500Hz(mOld1_5kHz);
            setEqValue_5kHz(mOld5kHz);
            setEqValue_10kHz(mOld10kHz);
            Wrapper.getWrapperInstance().setAudioMode(AUDIO_MODE_USER);
            
            callChangeListener(getEqValue_10kHz());
            Log.d(TAG, "onDialogClosed->ok getAudioMode" + Wrapper.getWrapperInstance().getAudioMode());
        } else {
            Log.d(TAG, "onDialogClosed->cancel getAudioMode----" + Wrapper.getWrapperInstance().getAudioMode());
        	restoreOldState();
            Wrapper.getWrapperInstance().setAudioMode(mOldAudioMode);
        }
        Log.d(TAG, "onDialogClosed-->positiveResult" + positiveResult);
    }

    private void restoreOldState() {
        if (mRestoredOldState) {
            return;
        }
        
        if (!Wrapper.getWrapperInstance().isAudioUserMode()) {
            return;
        }

        setEqValue_120Hz(mOldUser120Hz);
        setEqValue_500Hz(mOldUser500Hz);
        setEqValue_1500Hz(mOldUser1_5kHz);
        setEqValue_5kHz(mOldUser5kHz);
        setEqValue_10kHz(mOldUser10kHz);

        mRestoredOldState = true;
        Log.d(TAG, "EqValue--OldUser-->" + mOldUser120Hz + " " + mOldUser500Hz + " " + mOldUser1_5kHz + " " + mOldUser5kHz + " " + mOldUser10kHz);
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        final Parcelable superState = super.onSaveInstanceState();
        if (getDialog() == null || !getDialog().isShowing()) return superState;

        // Save the dialog state
        final SavedState myState = new SavedState(superState);
        myState.progress120hz = mSeekBar120Hz.getProgress();
        myState.progress500hz = mSeekBar500Hz.getProgress();
        myState.progress1_5khz = mSeekBar1_5kHz.getProgress();
        myState.progress5khz = mSeekBar5kHz.getProgress();
        myState.progress10khz = mSeekBar10kHz.getProgress();
        myState.oldProgress120hz = mOld120Hz;
        myState.oldProgress500hz = mOld500Hz;
        myState.oldProgress1_5khz = mOld1_5kHz;
        myState.oldProgress5khz = mOld5kHz;
        myState.oldProgress10khz = mOld10kHz;

        // Restore the old state when the activity or dialog is being paused
        restoreOldState();
        Log.d(TAG, "onSaveInstanceState-->myState=" + myState);
        return myState;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state == null || !state.getClass().equals(SavedState.class)) {
            // Didn't save state for us in onSaveInstanceState
            super.onRestoreInstanceState(state);
            Log.d(TAG, "onRestoreInstanceState-->myState=" + state);
            return;
        }

        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());
        mOld120Hz = myState.oldProgress120hz;
        mOld500Hz = myState.oldProgress500hz;
        mOld1_5kHz = myState.oldProgress1_5khz;
        mOld5kHz = myState.oldProgress5khz;
        mOld10kHz = myState.oldProgress10khz;
        
        setEqValue_120Hz(myState.progress120hz + MINIMUM_EQ);
        setEqValue_500Hz(myState.progress500hz + MINIMUM_EQ);
        setEqValue_1500Hz(myState.progress1_5khz + MINIMUM_EQ);
        setEqValue_5kHz(myState.progress5khz + MINIMUM_EQ);
        setEqValue_10kHz(myState.progress10khz + MINIMUM_EQ);
    }

    private static class SavedState extends BaseSavedState {

        int progress120hz;
        int oldProgress120hz;
        
        int progress500hz;
        int oldProgress500hz;
        
        int progress1_5khz;
        int oldProgress1_5khz;
        
        int progress5khz;
        int oldProgress5khz;
        
        int progress10khz;
        int oldProgress10khz;

        public SavedState(Parcel source) {
            super(source);
            progress120hz = source.readInt();
            oldProgress120hz = source.readInt();
            progress500hz = source.readInt();
            oldProgress500hz = source.readInt();
            progress1_5khz = source.readInt();
            oldProgress1_5khz = source.readInt();
            progress5khz = source.readInt();
            oldProgress5khz = source.readInt();
            progress10khz = source.readInt();
            oldProgress10khz = source.readInt();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeInt(progress120hz);
            dest.writeInt(oldProgress120hz);
            dest.writeInt(progress500hz);
            dest.writeInt(oldProgress500hz);
            dest.writeInt(progress1_5khz);
            dest.writeInt(oldProgress1_5khz);
            dest.writeInt(progress5khz);
            dest.writeInt(oldProgress5khz);
            dest.writeInt(progress10khz);
            dest.writeInt(oldProgress10khz);
        }

        public SavedState(Parcelable superState) {
            super(superState);
        }

        public static final Parcelable.Creator<SavedState> CREATOR =
                new Parcelable.Creator<SavedState>() {

            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }
}

